<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8"/>
    <title>layui</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <link rel="stylesheet" href="/lib/layui-v2.5.4/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all"/>
    <link rel="stylesheet" href="/css/public.css" media="all"/>
    <style>
        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }

        .layuimini-main {
            margin: 0px 10px 10px 0px;
        }

        .layui-tab {
            margin: 0;
            text-align: left !important;
        }

        .icon {
            margin-right: 10px;
            color: #1aa094;
        }

        .layui-card-header {
            background-color: #cbddf3;
            padding: 5px;
            font-size: 14px;
            border: 1px solid #99bce8;
            box-shadow: #f3f7fb 0 1px 0px 0 inset;
            height: 20px;
            line-height: 20px;
        }

        .layui-tab-content {
            padding: 5px !important;
        }

        .layui-card-body {
            height: 680px;
            border-color: #99bce8;
            border-width: 1px;
            border-style: solid;
            background: #fff;
        }

        .layui-card-table {
            padding: 0 !important;
            margin: 0 !important;
        }

        .layui-table-view, .layui-table {
            margin: 0 !important;
        }

        .layui-table td, .layui-table th {
            position: relative;
            padding: 5px 10px;
            min-height: 20px;
            line-height: 20px;
            font-size: 14px;
            color: #0C0C0C;
        }

        .layui-table-view .layui-table[lay-size=sm] th .layui-table-cell {
            line-height: 30px;
            height: 30px;
            font-weight: bold;
            font-size: 12px;
        !important:;
        }

        .layui-table-view .layui-table td, .layui-table-view .layui-table th {
            padding: 0;
            border-top: none;
            border-left: none;
        }

        .search-header {
            height: 30px;
            margin: 0;
            padding: 0;
            border-style: solid;
            border-color: #99bce8;
            border-width: 1px;
            background-color: #d3e1f1;
        }

        .layui-btn-search {
            /*border: 1px solid #C9C9C9;*/
            /*background-color: #fff;*/
            background-color: #d3e1f1;
            color: #0C0C0C;
        }

        .layui-nav {
            padding: 0;
            color: #0C0C0C !important;
        }

        .layui-nav .layui-nav-item a {
            display: block;
            padding-left: 5px;
            /*padding-right: 20px;*/
            color: #0C0C0C;
            transition: all .3s;
            -webkit-transition: all .3s;
            font-size: 12px !important;
        }

        dd a {
            padding: 0 20px !important;
        }

        /*.layui-nav-child{*/
        /*    padding: 0 20px !important;*/
        /*}*/
        .closeBox a span.layui-nav-more {
            border-color: #333 transparent transparent;
        }

        .closeBox .layui-nav-child {
            top: 34px;
        }

        .layui-table-click {
            background-color: #99BBFF !important;
        }

        .tableSelect {
            min-width: 0 !important;
        }
    </style>
</head>
<body>
<div class="layuimini-container" style="height:calc(100% - 4px)">
    <div class="layuimini-main">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
                <li class="layui-this">检验申请单维护</li>
                <li>检验标本管理</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-row layui-col-space3">
                        <div class="layui-col-md3">
                            <div class="grid-demo grid-demo-bg1">
                                <div class="layui-card">
                                    <div class="layui-card-header">
                                        <i class="fa fa-warning icon"></i>项目分类
                                    </div>
                                    <div class="search-header">
                                        <div class="layui-btn-group">

                                            <button type="button" id="refreshItemClass"
                                                    class="layui-btn layui-btn-search layui-btn-sm">
                                                刷新
                                            </button>
                                            <div class="layui-tab" lay-filter="layuiminiTab"
                                                 style="display: inline-block">
                                                <ul class="layui-nav closeBox">
                                                    <li class="layui-nav-item">
                                                        <a class="layui-btn layui-btn-search layui-btn-sm"
                                                           href="javascript:;">
                                                            操作</a>
                                                        <dl class="layui-nav-child"
                                                            style="z-index:999">
                                                            <dd><a href="javascript:;"
                                                                   data-page-close="other">
                                                                新增分类</a></dd>
                                                            <dd><a href="javascript:;"
                                                                   data-page-close="all">
                                                                修改分类</a></dd>
                                                            <dd><a href="javascript:;"
                                                                   data-page-close="all">
                                                                删除分类</a></dd>
                                                            <dd><a id="showItemClass">
                                                                查看信息</a></dd>
                                                        </dl>
                                                    </li>
                                                </ul>
                                            </div>
                                            <button type="button"
                                                    class="layui-btn layui-btn-search layui-btn-sm">
                                                绑定科室
                                            </button>
                                            <button type="button"
                                                    class="layui-btn layui-btn-search layui-btn-sm">
                                                调整序号
                                            </button>
                                        </div>
                                    </div>
                                    <div class="layui-card-body">
                                        <div id="applyItemClass" class="demo-tree"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md9">
                            <div class="grid-demo">
                                <div class="grid-demo grid-demo-bg1">
                                    <div class="layui-card">
                                        <div class="layui-card-header">
                                            <i class="fa fa-warning icon"></i>项目维护
                                        </div>
                                        <div class="layui-card-body" style="padding: 0 1px">
                                            <div class="layui-row layui-col-space3">
                                                <div class="layui-col-md3">
                                                    <div class="layui-card">
                                                        <div class="layui-card-header">
                                                            <i class="fa fa-warning icon"></i>申请单
                                                        </div>
                                                        <div class="layui-card-body layui-card-table"
                                                             style="height: 700px">
                                                            <div class="search-header">
                                                                <div class="layui-btn-group">
                                                                    <div class="layui-tab" lay-filter="layuiminiTab"
                                                                         id="top_tabs_box"
                                                                         style="display: inline-block">
                                                                        <ul class="layui-nav closeBox">
                                                                            <li class="layui-nav-item">
                                                                                <a class="layui-btn layui-btn-search layui-btn-sm"
                                                                                   href="javascript:;">
                                                                                    操作</a>
                                                                                <dl class="layui-nav-child"
                                                                                    style="z-index:999">
                                                                                    <dd><a href="javascript:;"
                                                                                           id="addApplyItemButton">
                                                                                        新增申请单</a></dd>
                                                                                    <dd><a href="javascript:;"
                                                                                           id="bindApplyItemButton">
                                                                                        绑定申请单</a></dd>
                                                                                    <dd><a href="javascript:;"
                                                                                           id="uptApplyItemButton">
                                                                                        修改申请单</a></dd>
                                                                                    <dd><a href="javascript:;"
                                                                                           id="delApplyItemButton">
                                                                                        删除申请单</a></dd>
                                                                                </dl>
                                                                            </li>
                                                                        </ul>
                                                                    </div>
                                                                    <button type="button" id="refreshApplyName"
                                                                            class="layui-btn layui-btn-search layui-btn-sm">
                                                                        刷新
                                                                    </button>
                                                                    <button type="button"
                                                                            class="layui-btn layui-btn-search layui-btn-sm">
                                                                        绑定科室
                                                                    </button>

                                                                </div>
                                                            </div>
                                                            <div>
                                                                <table class="layui-hide" id="applyTable"
                                                                       lay-filter="applyTable"></table>
                                                            </div>

                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="layui-col-md9">
                                                    <div class="layui-row layui-col-space3">
                                                        <div class="layui-col-md8">
                                                            <div class="layui-card">
                                                                <div class="search-header">
                                                                    <div class="layui-btn-group">
                                                                        <button type="button" id="addApplyItemInsp"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            新增
                                                                        </button>
                                                                        <button type="button" id="delApplyItemInsp"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            删除
                                                                        </button>
                                                                        <button type="button" style="color: darkgreen"
                                                                                id="setApplyItemInspUrgentTrue"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            启用加急
                                                                        </button>
                                                                        <button type="button" style="color: red"
                                                                                id="setApplyItemInspUrgentFalse"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            关闭加急
                                                                        </button>
                                                                    </div>
                                                                </div>

                                                                <div class="layui-card-body layui-card-table"
                                                                     style="height: 700px">
                                                                    <div>
                                                                        <table class="layui-hide" id="applyItemTable"
                                                                               lay-filter="applyItemTable"></table>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="layui-col-md4">
                                                            <div class="layui-card">

                                                                <div class="search-header">
                                                                    <div class="layui-btn-group">
                                                                        <button type="button" id="addApplySample"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            新增
                                                                        </button>
                                                                        <button type="button" id="delApplySample"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            删除
                                                                        </button>
                                                                        <button type="button" id="uptSampleOrderNo"
                                                                                class="layui-btn layui-btn-search layui-btn-sm">
                                                                            调整序号
                                                                        </button>
                                                                    </div>
                                                                </div>

                                                                <div class="layui-card-body layui-card-table"
                                                                     style="height: 700px">
                                                                    <div>
                                                                        <table class="layui-hide" id="applySampleTable"
                                                                               lay-filter="applySampleTable"></table>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-tab-item">
                    <div class="layui-row layui-col-space3">
                        <div class="layui-col-md6">
                            <div>
                                <table class="layui-table" id="sampleDetailTable1"
                                       lay-filter="applySampleTable1"></table>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div>
                                <div class="layui-input-inline">

                                </div>
                            </div>
                        </div>
                    </div>


                </div>
            </div>
        </div>
        <div hidden="hidden" id="sampleTable">
            <table class="layui-table" id="sampleDetailTable"></table>
        </div>

        <div hidden="hidden" id="applyTableUi">
            <table class="layui-table" id="applyTable1"></table>
        </div>

        <div hidden="hidden" id="searchClinicItem">
            <div class="tableSelectBar" style="padding: 5px">
                <form class="layui-form" action="">
                    <input style="display:inline-block;width:190px;height:30px;vertical-align:middle;border: 1px solid #C9C9C9;"
                           type="text" name="inputCode" placeholder="关键词搜索" autocomplete="off" class="layui-input">
                    <button class="layui-btn layui-btn-sm layui-btn-primary tableSelect_btn_search" lay-submit
                            lay-filter="tableSelect_btn_search">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                    </input>
                </form>
            </div>
            <table class="layui-table" id="searchClinicItemTable"></table>
        </div>
        <div hidden="hidden" id="addApplyItemUi">
            <div class="tableSelectBar" style="padding: 5px">
                <form class="layui-form" action="" lay-filter="addApplyItemForm">
                    <div class="layui-form-item" style="margin-right: 30px;">
                        <label class="layui-form-label">申请单名称:</label>
                        <div class="layui-input-block">
                            <input type="text" name="applyItemName" id="applyItemName" lay-verify="required"
                                   lay-reqtext="分类名称不能为空" placeholder="请输入" class="layui-input"></input>
                        </div>
                    </div>
                    <div class="layui-form-item" style="margin-right: 30px;">
                        <label class="layui-form-label">执行科室:</label>
                        <div class="layui-input-block">
                            <input type="text" name="execOrgName" id="execOrgName" lay-verify="required"
                                   lay-reqtext="分类名称不能为空" placeholder="请输入" class="layui-input"></input>
                        </div>
                    </div>
                    <input type="hidden" name="execOrgCode" id="execOrgCode"></input>
                    <input type="hidden" name="catalogId" id="catalogId"></input>
                    <input type="hidden" name="applyId" id="applyId"></input>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>

    layui.use(['element', 'tree', 'table', 'form', 'tableSelect'], function () {
        var $ = layui.jquery,
            element = layui.element,
            table = layui.table,
            tree = layui.tree,
            form = layui.form,
            tableSelect = layui.tableSelect
        ;

        $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
            /*当单击表格行时,把单选按钮设为选中状态*/
            var tableDiv;
            var index = $(this).attr('data-index');
            var tableBox = $(this).parents('.layui-table-box');
            //存在固定列
            if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
            } else {
                tableDiv = tableBox.find(".layui-table-body.layui-table-main");
            }
            var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.layui-table-cell div.layui-form-radio I");
            if (checkCell.length > 0) {
                checkCell.click();
            }
        });
        //对td的单击事件进行拦截停止，防止事件冒泡再次触发上述的单击事件  将此代码在页面初始化后执行一次即可以。
        $(document).on("click", "td div.layui-table-cell div.layui-form-radio", function (e) {
            e.stopPropagation();
        });

        //单击行勾选checkbox事件
        $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
            /*当单击表格行时,把单选按钮设为选中状态*/
            var tableDiv;
            var index = $(this).attr('data-index');
            var tableBox = $(this).parents('.layui-table-box');
            //存在固定列
            if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
            } else {
                tableDiv = tableBox.find(".layui-table-body.layui-table-main");
            }
            var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
            if (checkCell.length > 0) {
                checkCell.click();
            }
        });

        //对td的单击事件进行拦截停止，防止事件冒泡再次触发上述的单击事件
        $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
            e.stopPropagation();
        });


        //方法渲染：
        table.render({
            elem: '#applyTable',
            method: 'post',
            url: '/emr/getApplyItemDetailByClass',
            data: [],
            cols: [ [
                {type: 'radio', fixed: 'left'},
                {field: 'applyItemName', title: '申请单名称', sort: true},
                {field: 'applyId', title: '申请单ID', sort: true, hide: true}
            ]],
            size: 'sm',   //尺寸
            skin: 'line',  //line （行边框风格）  row （列边框风格） nob （无边框风格)
            height: '670',
            even: true  //隔行背景
        });

        tableSelect.render({
            elem: '#execOrgName',	//定义输入框input对象 必填
            checkedKey: 'code', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'keyword',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: '/bds/getBdsOrganization',
                cols: [ [
                    {type: 'radio', fixed: 'left'},
                    {field: 'code', title: '科室编码', sort: true},
                    {field: 'name', title: '科室名称', sort: true}
                ]]
            },
            done: function (elem, data) {
                if (data) {
                    elem.val(data.data[0].name);
                    $('#execOrgCode').val(data.data[0].code);
                }
            }
        })

        let applyClassData = [];

        $(function () {
            loadApplyItemClass();
        })

        let currentApplyItem = null;

        function loadApplyItemClass() {
            $.ajax({
                url: "/emr/getApplyItemClass",
                type: "POST",
                success: function (obj) {
                    applyClassData = obj;
                    applyClassData[0].spread = true;
                    tree.render({
                        id: 'applyItemClass',
                        elem: '#applyItemClass'
                        , data: applyClassData
                        , onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                        , click: function (obj) {
                            $('#execOrgName').attr('disabled', 'disabled');
                            $('#execOrgName').addClass('layui-disabled');
                            $('#execOrgName').val(obj.data.title);
                            $('#execOrgCode').val(obj.data.execOrgCode);
                            $('#catalogId').val(obj.data.catalogId);

                            currentApplyItem = obj;

                            //执行搜索重载
                            table.reload('applyTable', {
                                where: {
                                    execOrgCode: obj.data.execOrgCode,
                                    catalogId: obj.data.catalogId
                                }
                            });
                            table.reload('applyItemTable', {
                                where: {
                                    applyId: ''
                                }
                            });
                            table.reload('applySampleTable', {
                                where: {
                                    dictInspecSampleId: '0'
                                }
                            });
                        }
                    });
                }
            });
        }

        $('#refreshItemClass').on('click', function () {
            loadApplyItemClass();
            form.val('addApplyItemForm', {
                execOrgCode: '',
                execOrgName: '',
                applyItemName: '',
                catalogId: ''
            });
            $('#execOrgName').removeAttr('disabled');
            $('#execOrgName').removeClass('layui-disabled');
            table.reload('applyTable', {
                where: {
                    execOrgCode: '',
                    catalogId: ''
                }
            });
            table.reload('applyItemTable', {
                where: {
                    applyId: ''
                }
            });
            table.reload('applySampleTable', {
                dictInspecSampleId: null
            });
        })

        $('#showItemClass').on('click', function () {
            layer.alert(
                JSON.stringify(currentApplyItem.data)
            );
        })

        $('#refreshApplyName').on('click', function () {
            table.reload('applyTable', {});
            table.reload('applyItemTable', {
                where: {
                    applyId: ''
                }
            });
            table.reload('applySampleTable', {
                where: {
                    dictInspecSampleId: '0'
                }
            });
        })

        //监听行单击事件
        table.on('row(applyTable)', function (obj) {
            //执行搜索重载
            table.reload('applyItemTable', {
                where: {
                    applyId: obj.data.applyId
                }
            });
            table.reload('applySampleTable', {
                where: {
                    dictInspecSampleId: '0'
                }
            });
        });

        table.render({
            elem: '#applyItemTable',
            method: 'post',
            url: '/emr/getApplyInspByApplyId',
            cols: [ [
                {type: "radio", fixed: "left"},
                {field: 'clinicItemName', title: '项目', width: '60%', sort: true},
                {field: 'dictInspecSampleId', hide: 'true'},
                {
                    title: '加急', width: '30%', templet: function (d) {
                        if (d.haveUrgent == "1" || d.haveUrgent == 1) {
                            return '<span style="color: darkgreen">允许</span>';
                        } else {
                            return '<span style="color: red">禁止</span>';
                        }
                    }, align: "center"
                }
            ]],
            size: 'sm',
            skin: 'line',
            height: '700',
            even: true
        });
        table.render({
            elem: "#sampleDetailTable",
            method: "post",
            url: "/emr/getAllSampleDetail",
            height: 'full-200',
            cols: [ [
                {type: "checkbox", width: "10%", fixed: "left"},
                {field: "itemName", width: "60%", title: "标本名称"},
                {field: "itemValue", width: "30%", title: "标本ID"}
            ]],
            size: 'sm',
            skin: 'line',
            even: true
        });

        //监听行单击事件
        table.on('row(applyItemTable)', function (obj) {
            $(obj).parent().find('tr .layui-table-click').removeClass('layui-table-click');
            // $('.layui-table-body table.layui-table tbody tr').removeClass('layui-table-click');
            obj.tr.addClass('layui-table-click');
            table.reload('applySampleTable', {
                where: {
                    dictInspecSampleId: obj.data.dictInspecSampleId
                }
            });
        });

        table.render({
            elem: '#applySampleTable',
            method: 'post',
            url: '/emr/getApplySampleTypeByInspId',
            cols: [ [
                {type: "checkbox", fixed: "left"},
                {field: 'sampleName', title: '标本名称', sort: true},
                {field: 'displayOrder', title: '序号', sort: true},
                {field: 'dictInspecSampleId', title: '项目ID', hide: 'true'},
                {field: 'sampleType', title: '标本类型', hide: 'true'}
            ]],
            size: 'sm',
            skin: 'line',
            height: '700',
            even: true
        });

        table.render({
            elem: '#applyTable1',
            method: 'post',
            url: '/emr/getAllApplyItemDetail',
            cols: [ [
                {type: 'checkbox', fixed: 'left'},
                {field: 'applyItemName', title: '申请单名称', sort: true},
                {field: 'applyId', title: '申请单ID', sort: true, hide: true}
            ]],
            size: 'sm',   //尺寸
            skin: 'line',  //line （行边框风格）  row （列边框风格） nob （无边框风格)
            even: true  //隔行背景
        });

        table.render({
            elem: '#searchClinicItemTable',	//定义输入框input对象 必填
            url: '/bds/getBdsClinicItemListByInputCode',
            cols: [ [
                {type: "checkbox", fixed: "left"},
                {field: "itemName", width: '60%', title: "项目名称"},
                {field: "itemCode", title: "项目编码"},
                {field: "id", title: "项目ID", hide: 'true'}
            ]],
            limit: 10,
            page: true,
            size: 'sm',
            skin: 'line',
            even: true
        })

        $('#setApplyItemInspUrgentTrue').on('click', function () {
            setApplyItemInsp(1);
        })

        $('#setApplyItemInspUrgentFalse').on('click', function () {
            setApplyItemInsp(0);
        })

        function setApplyItemInsp(flag) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            let rowObj = table.checkStatus('applyItemTable').data[0];
            $.ajax({
                url: "/emr/uptApplyInspRelNew",
                type: "POST",
                data: {
                    applyId: rowObj.applyId,
                    dictInspecSampleId: rowObj.dictInspecSampleId,
                    haveUrgent: flag
                },
                success: function (obj) {
                    layer.close(index);
                    table.reload('applyItemTable', {});
                }
            });
        }

        $('#uptSampleOrderNo').on('click', function () {
            setApplySampleOrderNo();
        })

        function setApplySampleOrderNo() {
            //prompt层
            layer.prompt({title: '修改标本序号'}, function (text, index) {
                layer.close(index);
                index = layer.msg('服务器处理中...', {
                    icon: 16
                    , shade: 0.08,
                    time: 0
                });
                let rowObj = table.checkStatus('applySampleTable').data[0];
                $.ajax({
                    url: "/emr/uptApplySample",
                    type: "POST",
                    data: {
                        dictInspecSampleId: rowObj.dictInspecSampleId,
                        sampleType: rowObj.sampleType,
                        displayOrder: text
                    },
                    success: function (obj) {
                        layer.close(index);
                        table.reload('applySampleTable', {});
                    }
                });
            });
        }

        $('#addApplySample').on('click', function () {
            addApplySample();
        })

        function addSampleRequest(obj) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            // console.info(JSON.stringify(obj));
            $.ajax({
                url: "/emr/addSample",
                type: "POST",
                data: {
                    sampleArray: JSON.stringify(obj)
                },
                success: function (obj) {
                    layer.close(index);
                    table.reload('applySampleTable', {});
                }
            });
        }

        $('#delApplySample').on('click', function () {
            delApplySample();
        });

        function delApplySample() {
            let sampleArray = table.checkStatus('applySampleTable').data;
            if (sampleArray) {
                var index = layer.msg('服务器处理中...', {
                    icon: 16
                    , shade: 0.08,
                    time: 0
                });
                $.ajax({
                    url: "/emr/delSample",
                    type: "POST",
                    data: {
                        sampleArray: JSON.stringify(sampleArray)
                    },
                    success: function (obj) {
                        layer.close(index);
                        table.reload('applySampleTable', {});
                    }
                });
            } else {
                layer.alert('请先选择要删除的检验标本信息', {
                    icon: 2
                })
            }
        }

        $('#addApplyItemInsp').on('click', function () {
            addApplyItemInsp();
        })


        //关键词搜索
        form.on('submit(tableSelect_btn_search)', function (data) {
            table.reload('searchClinicItemTable', {
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        function addApplyItemInspRequest(obj) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            // console.info(JSON.stringify(obj));
            $.ajax({
                url: "/emr/addApplyItemInsp",
                type: "POST",
                data: {
                    inspArray: JSON.stringify(obj)
                },
                success: function (obj) {
                    layer.close(index);
                    table.reload('applyItemTable', {});
                    table.reload('applySampleTable', {
                        where: {
                            dictInspecSampleId: ''
                        }
                    });
                }
            });
        }

        $('#delApplyItemInsp').on('click', function () {
            delApplyItemInsp();
        })


        function delApplyItemInsp() {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            let rowObj = table.checkStatus('applyItemTable').data[0];
            if (rowObj) {
                $.ajax({
                    url: "/emr/delApplyItemInsp",
                    type: "POST",
                    data: {
                        "applyId": rowObj.applyId,
                        "clinicItemName": rowObj.clinicItemName,
                        "clinicalItemId": rowObj.clinicalItemId,
                        "dictInspecSampleId": rowObj.dictInspecSampleId,
                        "haveUrgent": rowObj.haveUrgent
                    },
                    success: function (obj) {
                        layer.close(index);
                        table.reload('applyItemTable', {});
                        table.reload('applySampleTable', {
                            where: {
                                dictInspecSampleId: ''
                            }
                        });
                    }
                });
            } else {
                layer.alert('请先选择一项申请单', {
                    icon: 2
                })
            }
        }

        function addApplySample() {
            let rowObj = table.checkStatus('applyItemTable').data[0];
            if (rowObj) {
                layer.open({
                    type: 1,
                    title: '新增检验标本-' + rowObj.clinicItemName,
                    content: $("#sampleTable"),
                    moveOut: true,
                    move: true,
                    // area:'460px',
                    success: function () {
                        table.render({
                            elem: "#sampleDetailTable",
                            method: "post",
                            url: "/emr/getAllSampleDetail",
                            height: 'full-200',
                            cols: [ [
                                {type: "checkbox", width: "10%", fixed: "left"},
                                {field: "itemName", width: "60%", title: "标本名称"},
                                {field: "itemValue", width: "30%", title: "标本ID"}
                            ]],
                            size: 'sm',
                            skin: 'line',
                            even: true
                        });
                    },
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        let sampleArray = table.checkStatus('sampleDetailTable').data;
                        let result = sampleArray.map(item => {
                            return {
                                sampleType: item.itemValue,
                                dictInspecSampleId: rowObj.dictInspecSampleId,
                                displayOrder: null,
                                sampleName: null
                            }
                        })
                        layer.close(index);
                        addSampleRequest(result);
                    }
                });
            } else {
                layer.alert('请先选择一项检验项目', {
                    icon: 2
                })
            }
        }

        function addApplyItemInsp() {
            let rowObj = table.checkStatus('applyTable').data[0];
            if (rowObj) {
                layer.open({
                    type: 1,
                    // area: ['923px','466px'],
                    area: '94%',
                    title: '添加项目',
                    content: $("#searchClinicItem"),
                    moveOut: true,
                    success: function (layero, index) {
                        table.render({
                            elem: '#searchClinicItemTable',	//定义输入框input对象 必填
                            url: '/bds/getBdsClinicItemListByInputCode',
                            cols: [ [
                                {type: "checkbox", fixed: "left"},
                                {field: "itemName", width: '60%', title: "项目名称"},
                                {field: "itemCode", title: "项目编码"},
                                {field: "id", title: "项目ID", hide: 'true'}
                            ]],
                            limit: 10,
                            page: true,
                            size: 'sm',
                            skin: 'line',
                            even: true
                        })
                    },
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        let clinicArray = table.checkStatus('searchClinicItemTable').data;
                        let result = clinicArray.map(item => {
                            return {
                                clinicalItemId: item.id,
                                applyId: rowObj.applyId,
                                haveUrgent: null,
                                displayOrder: null,
                                dictInspecSampleId: null
                            }
                        })
                        layer.close(index);
                        addApplyItemInspRequest(result);
                    }
                });
            } else {
                layer.alert('请先选择一项申请单', {
                    icon: 2
                })
            }
        }

        function addApplyItem() {
            layer.open({
                type: 1,
                // area: ['923px','466px'],
                title: '添加申请单',
                content: $("#addApplyItemUi"),
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    //console.info(form.val('addApplyItemForm'));
                    layer.close(index);
                    addApplyItemRequest(form.val('addApplyItemForm'));
                }
            });
        }

        function addApplyItemRequest(obj) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            // console.info(JSON.stringify(obj));
            $.ajax({
                url: "/emr/addApplyItem",
                type: "POST",
                data: {
                    catalogId: obj.catalogId,
                    execOrgCode: obj.execOrgCode,
                    applyItemName: obj.applyItemName,
                    applyType: 'C'
                },
                success: function (obj) {
                    layer.close(index);
                    form.val('addApplyItemForm', {
                        applyItemName: ''
                    });
                    table.reload('applyTable');
                    table.reload('applyItemTable', {
                        where: {
                            applyId: ''
                        }
                    });
                    table.reload('applySampleTable', {
                        where: {
                            dictInspecSampleId: ''
                        }
                    });
                }
            });
        }

        function uptApplyItemRequest(obj) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            // console.info(JSON.stringify(obj));
            $.ajax({
                url: "/emr/saveApplyItem",
                type: "POST",
                data: {
                    applyItemName: obj.applyItemName,
                    applyId: obj.applyId
                },
                success: function (obj) {
                    layer.close(index);
                    form.val('addApplyItemForm', {
                        applyItemName: ''
                    });
                    table.reload('applyTable');
                    table.reload('applyItemTable', {
                        where: {
                            applyId: ''
                        }
                    });
                    table.reload('applySampleTable', {
                        where: {
                            dictInspecSampleId: ''
                        }
                    });
                }
            });
        }

        function uptApplyItem() {
            let rowObj = table.checkStatus('applyTable').data[0];
            if (rowObj) {
                form.val('addApplyItemForm', {
                    execOrgCode: '',
                    // execOrgName: '',
                    applyItemName: rowObj.applyItemName,
                    catalogId: '',
                    applyId: rowObj.applyId
                });
                layer.open({
                    type: 1,
                    // area: ['923px','466px'],
                    title: '添加申请单',
                    content: $("#addApplyItemUi"),
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        //console.info(form.val('addApplyItemForm'));
                        layer.close(index);
                        uptApplyItemRequest(form.val('addApplyItemForm'));
                    }
                });
            } else {
                layer.alert('请选择要修改的申请单', {
                    icon: 2
                })
            }
        }

        function bindApplyItemRequest(obj) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            // console.info(JSON.stringify(obj));
            $.ajax({
                url: "/emr/bindApplyItem",
                type: "POST",
                data: {
                    CpoeAppCaRelVstArray: JSON.stringify(obj)
                },
                success: function (obj) {
                    layer.close(index);
                    form.val('addApplyItemForm', {
                        applyItemName: ''
                    });
                    table.reload('applyTable');
                    table.reload('applyItemTable', {
                        where: {
                            applyId: ''
                        }
                    });
                    table.reload('applySampleTable', {
                        where: {
                            dictInspecSampleId: ''
                        }
                    });
                }
            });
        }

        function bindApplyItem() {
            let execOrgCode = $('#execOrgCode').val();
            let catalogId = $('#catalogId').val();
            if (catalogId || execOrgCode) {
                layer.open({
                    type: 1,
                    // area: ['923px','466px'],
                    title: '选择需要绑定的申请单 - ' + $('#execOrgName').val(),
                    content: $("#applyTableUi"),
                    success: function () {
                        table.render({
                            elem: '#applyTable1',
                            method: 'post',
                            url: '/emr/getAllApplyItemDetail',
                            cols: [ [
                                {type: 'checkbox', fixed: 'left'},
                                {field: 'applyItemName', title: '申请单名称', sort: true},
                                {field: 'applyId', title: '申请单ID', sort: true, hide: true}
                            ]],
                            size: 'sm',   //尺寸
                            skin: 'line',  //line （行边框风格）  row （列边框风格） nob （无边框风格)
                            even: true  //隔行背景
                        });
                    },
                    btn: ['绑定', '取消'],
                    yes: function (index, layero) {
                        layer.close(index);
                        let applyArray = table.checkStatus('applyTable1').data;
                        let result = applyArray.map(item => {
                            return {
                                catalogId: catalogId,
                                execOrgCode: execOrgCode,
                                applyId: item.applyId,
                                clinicalItemId: null
                            }
                        })
                        bindApplyItemRequest(result);
                    }
                });
            } else {
                layer.alert('请选择一项分类', {
                    icon: 2
                })
            }
        }

        $('#uptApplyItemButton').on('click', function () {
            uptApplyItem();
        })

        $('#bindApplyItemButton').on('click', function () {
            bindApplyItem();
        })


        $('#addApplyItemButton').on('click', function () {
            addApplyItem();
        })

        $('#delApplyItemButton').on('click', function () {
            delApplyItem();
        })

        function delApplyItem() {
            let rowObj = table.checkStatus('applyTable').data[0];
            if (rowObj) {
                layer.confirm('确定要删除申请单 ' + rowObj.applyItemName + ' ？申请单绑定的项目会一并删除！', function () {
                    delApplyItemRequest(rowObj);
                });
            } else {
                layer.alert('请选择要删除的申请单', {
                    icon: 2
                })
            }
        }

        function delApplyItemRequest(obj) {
            var index = layer.msg('服务器处理中...', {
                icon: 16
                , shade: 0.08,
                time: 0
            });
            // console.info(JSON.stringify(obj));
            $.ajax({
                url: "/emr/delApplyItem",
                type: "POST",
                data: {
                    applyId: obj.applyId
                },
                success: function (obj) {
                    layer.close(index);

                    table.reload('applyTable');
                    table.reload('applyItemTable');
                    table.reload('applySampleTable', {
                        where: {
                            dictInspecSampleId: ''
                        }
                    });
                }
            });
        }

        element.on('tab(docDemoTabBrief)', function (data) {
            if (data.index == 1) {
                table.render({
                    elem: "#sampleDetailTable1",
                    method: "post",
                    url: "/emr/getAllSampleDetail",
                    height: '720',
                    cols: [ [
                        {type: "checkbox", width: "10%", fixed: "left"},
                        {field: "itemName", width: "60%", title: "标本名称"},
                        {field: "itemValue", width: "30%", title: "标本ID"}
                    ]],
                    size: 'sm',
                    skin: 'line',
                    even: true
                });
            }

        });
    });
</script>

</body>
</html>
